export default {
  namespaced: true,
  state: {
    address: uni.getStorageSync('address'), // 收货地址
    token: uni.getStorageSync('token'),
    // 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjU0LCJpYXQiOjE2NDI4MjA0NjcsImV4cCI6MTAwMTY0MjgyMDQ2Nn0.euo_T0z46njSNZdElnVx-4IDPhl4oibXMR2gaxoHQwY', // uni.getStorageSync('token'), // token
    redirectInfo: null, // 记录登录成功之后，跳转到哪里去
    userInfo: uni.getStorageSync('userInfo') // 用户信息
  },
  getters: {
    getaddress (state) {
      return state.address
    },
    getToken (state) {
      return state.token
    },
    getRedirectInfo (state) {
      return state.redirectInfo
    },
    getuserInfo (state) {
      return state.userInfo
    },
    addressStr (state) {
      return (
        state.address &&
        `${state.address.provinceName}${state.address.cityName}${state.address.countyName}${state.address.detailInfo}`
      )
    }
  },
  mutations: {
    setaddress (state, address) {
      state.address = address

      // 保存到本地
      uni.setStorageSync('address', address)
    },
    setToken (state, token) {
      state.token = token

      // 保存到本地
      uni.setStorageSync('token', token)
    },
    setuserInfo (state, userInfo) {
      state.userInfo = userInfo

      // 保存到本地
      uni.setStorageSync('userInfo', userInfo)
    },
    setRedirectInfo (state, redirectInfo) {
      state.redirectInfo = redirectInfo
    }
  },
  actions: {
    esc ({ commit }) {
      // 模拟发送请求给后台，告知后台，前端要退出，你该怎么处理就怎么处理
      setTimeout(() => {
        // 清除token
        commit('setToken', null)
        // 清除vuex中的用户信息
        commit('setuserInfo', null)
        // 跳转到登录页面（我们这个地方不需要管它，因为只要把token设为null，则自动切换成登录子组件）
      }, 500)
    }
  }
}
